package com.muapet.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;

import com.muapet.mapper.PetMapper;
import com.muapet.mapper.VarietyMapper;
import com.muapet.model.Pet;
import com.muapet.model.Variety;
import com.muapet.model.other.Pagination;
import com.muapet.model.other.PetCondition;
import com.muapet.service.IPetService;

@Service
public class PetServiceImpl implements IPetService {

	private PetMapper petMapper;
	private VarietyMapper varietyMapper;

	@Autowired
	public void setPetMapper(PetMapper petMapper) {
		this.petMapper = petMapper;
	}

	@Autowired
	public void setVarietyMapper(VarietyMapper varietyMapper) {
		this.varietyMapper = varietyMapper;
	}

	@Override
	public List<Pet> listPetByConditions(PetCondition conditions) {
		String title = conditions.getTitle();
		Integer sex = conditions.getSex();
		Integer type = conditions.getType();
		Double lowPrice = conditions.getLowPrice();
		Double highPrice = conditions.getHighPrice();
		List<String> variety = null;
		if (conditions.getVarietys() != null) {
			variety = Arrays.asList(conditions.getVarietys());
		}
		String sortCondition = conditions.getSortCondition();
		// 分页条件
		Integer pageIndex = conditions.getPageIndex();
		Integer numIndex = conditions.getNumIndex();
		Example petExample = new Example(Pet.class);
		if (title != null && !title.isEmpty()) {
			petExample.and().andLike("title", "%" + title + "%");
		}
		if (sex != null && sex != 0) {
			petExample.and().andEqualTo("sex", sex);
		}
		if (type != null && type != 0) {
			petExample.and().andEqualTo("type", type);
		}
		// 默认价格精度到小数点后面两位，double无法==判断，以此判断double是否真实有值
		if (lowPrice != null && lowPrice > 0.001) {
			petExample.and().andGreaterThanOrEqualTo("price", lowPrice);
		}
		if (highPrice != null && highPrice > 0.001) {
			petExample.and().andLessThanOrEqualTo("price", highPrice);
		}
		if (variety != null && variety.size() > 0
		// 全选时传筛选类别所有品种，用in方法查条件多余且效率低
				&& variety.size() < listVarietysByType(type).size()) {
			petExample.and().andIn("variety", variety);
		}
		petExample.and().andIsNull("buyerId").orEqualTo("buyerId", 0);
		if (sortCondition != null && !sortCondition.isEmpty()) {
			petExample.orderBy(sortCondition);
		}
		if (pageIndex == null || pageIndex == 0) {
			pageIndex = 1;
		}
		if (numIndex == null || numIndex == 0) {
			numIndex = 20;
		}
		return petMapper.selectByExampleAndRowBounds(petExample, new RowBounds(
				((pageIndex - 1) * numIndex), numIndex));
	}

	@Override
	public Integer countPetByConditions(PetCondition conditions) {
		String title = conditions.getTitle();
		Integer sex = conditions.getSex();
		Integer type = conditions.getType();
		Double lowPrice = conditions.getLowPrice();
		Double highPrice = conditions.getHighPrice();
		List<String> variety = null;
		if (conditions.getVarietys() != null) {
			variety = Arrays.asList(conditions.getVarietys());
		}
		Example petExample = new Example(Pet.class);
		if (title != null && !title.isEmpty()) {
			petExample.and().andLike("title", "%" + title + "%");
		}
		if (sex != null && sex != 0) {
			petExample.and().andEqualTo("sex", sex);
		}
		if (type != null && type != 0) {
			petExample.and().andEqualTo("type", type);
		}
		// 默认价格精度到小数点后面两位，double无法==判断，以此判断double是否真实有值
		if (lowPrice != null && lowPrice > 0.001) {
			petExample.and().andGreaterThanOrEqualTo("price", lowPrice);
		}
		if (highPrice != null && highPrice > 0.001) {
			petExample.and().andLessThanOrEqualTo("price", highPrice);
		}
		if (variety != null && variety.size() > 0
		// 全选时传筛选类别所有品种，用in方法查条件多余且效率低
				&& variety.size() < listVarietysByType(type).size()) {
			petExample.and().andIn("variety", variety);
		}
		petExample.and().andIsNull("buyerId").orEqualTo("buyerId", 0);

		return petMapper.selectCountByExample(petExample);
	}

	@Override
	public List<Pet> listNewestPet() {
		Example petExample = new Example(Pet.class);
		petExample.and().andIsNull("buyerId").orEqualTo("buyerId", 0);
		petExample.orderBy("createTime").desc();
		return petMapper.selectByExampleAndRowBounds(petExample, new RowBounds(
				0, 12));
	}

	@Override
	public List<Variety> listVarietysByType(Integer type) {
		Variety variety = new Variety();
		if (type != null && type != 0) {
			variety.setType(type);
		}
		return varietyMapper.select(variety);
	}

	@Override
	public Pet findById(Integer id) {
		return petMapper.selectByPrimaryKey(id);
	}

	@Override
	public Integer update(Pet pet) {
		return petMapper.updateByPrimaryKeySelective(pet);
	}

	@Override
	public Integer add(Pet pet) {
		setPetDefaultParam(pet);
		return petMapper.insertSelective(pet);
	}

	/**
	 * 设置默认值
	 * 
	 * @param user
	 * @return
	 */
	private Pet setPetDefaultParam(Pet pet) {
		pet.setCreateTime(new Date());
		return pet;
	}

	@Override
	public Integer delete(Integer id) {
		return petMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Pet> listPetBySeller(Integer sellerId, Integer state) {
		Example petExample = new Example(Pet.class);
		petExample.and().andEqualTo("sellerId", sellerId);
		if (state != null && state == 1) {
			petExample.and().andIsNull("buyerId").orEqualTo("buyerId", 0);
		} else if (state != null && state == 2) {
			petExample.and().andGreaterThan("buyerId", 0);
		}
		petExample.orderBy("createTime").desc();
		return petMapper.selectByExample(petExample);
	}

	@Override
	public Integer updateVariety(Variety variety) {
		return varietyMapper.updateByPrimaryKeySelective(variety);
	}

	@Override
	public Integer addVariety(Variety variety) {
		return varietyMapper.insertSelective(variety);
	}

	@Override
	public Integer deleteVariety(Integer id) {
		return varietyMapper.deleteByPrimaryKey(id);
	}

	@Override
	public Variety findVarietyById(Integer id) {
		return varietyMapper.selectByPrimaryKey(id);
	}

	@Override
	public Boolean checkVariety(String name) {
		Variety variety = new Variety();
		variety.setVarietyName(name);
		return varietyMapper.selectCount(variety) == 0;
	}

	@Override
	public List<Pet> findAllPet(Pagination pagination) {
		return petMapper.selectByRowBounds(null,
				new RowBounds(pagination.getOffset(), pagination.getLimit()));
	}

	@Override
	public Integer countAllPet() {
		return petMapper.selectCount(null);
	}
}
